package sg.edu.nus.iss.vmcs.maintenance;

/*
 * Copyright 2003 ISS.
 * The contents contained in this document may not be reproduced in any
 * form or by any means, without the written permission of ISS, other
 * than for the purpose for which it has been supplied.
 *
 */

import java.awt.event.*;
import java.awt.*;
import java.util.ArrayList;

import sg.edu.nus.iss.vmcs.store.*;
import sg.edu.nus.iss.vmcs.util.VMCSException;

/**
 *
 *
 * @version 3.0 5/07/2003
 * @author Olivo Miotto, Pang Ping Li
 */

public class ButtonItemDisplay extends Panel {

	private ButtonItem items[];
	private StoreItem[] storeItems;
	private int len;
	private Label lb;
	
	public ButtonItemDisplay(String title, StoreItemIterator itr){
		StoreItem[] sItems = convStoreItemIteratorToArray(itr);
		createButtons(title,sItems, sItems.length);
	}
	
	private StoreItem[] convStoreItemIteratorToArray(StoreItemIterator itr){
		ArrayList<StoreItem> ls = new ArrayList<StoreItem>();
		while( itr.hasNext()){
			ls.add(itr.next());
		}
		StoreItem[] sItems = new StoreItem[ls.size()];
		sItems = ls.toArray(sItems);
		return sItems;
	}
	
	private void createButtons(String title, StoreItem sitem[], int length){
		len = length;
		this.storeItems = sitem;
		System.out.println("ButtonItemDisplay:" + len);
		Panel tp1 = new Panel();

		lb = new Label(title);
		tp1.add(lb);

		this.setLayout(new GridLayout(0, 1));

		this.add(tp1);

		int i;
		items = new ButtonItem[len];

		for (i = 0; i < len; i++) {
			StoreObject ob = sitem[i].getContent();

			items[i] =
				new ButtonItem(
					ob.getName(),
					ButtonItem.DEFAULT_LEN,
					ButtonItem.GRID);
			this.add(items[i]);
		}		
	}

	public ButtonItemDisplay(String title, StoreItem sitem[], int length) {
		createButtons(title,sitem, length);
	}

	public void addListener(ActionListener l) {
		int i;
		for (i = 0; i < len; i++) {
			items[i].addListener(l);
			items[i].setActionCommand(String.valueOf(i));
		}
	}

	public void setActive(boolean st) {
		int i;
		lb.setEnabled(st);
		for (i = 0; i < len; i++) {
			items[i].setActive(st);
		}
	}

	public void clear() {
		int i;
		for (i = 0; i < len; i++) {
			items[i].clear();
		}
	}
	
	public void sort(StoreItemIterator itr){
		storeItems = convStoreItemIteratorToArray( itr );
		for (int i = 0; i < len; i++) {
			items[i].setBtnLabel(storeItems[i].getContent().getName());
			try {
				displayQty(i, 0);
			} catch (VMCSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void displayQty(int idx, int qty) throws VMCSException {
		System.out.println("ButtonItemDisplay:" + idx);
		if (idx >= len)
			throw new VMCSException("ButtomDisplay.setQty", "Index over flow");

//		items[idx].setValue(qty);
		qty = storeItems[idx].getQuantity();
		items[idx].setValue(qty);
	}

}
